//
//  ValidateCarNum.m
//  Towatt_01
//
//  Created by TwtMac on 17/3/24.
//  Copyright © 2017年 Mazy. All rights reserved.
//

#import "ValidateCarNum.h"

@implementation ValidateCarNum

+(BOOL)checkIsCarNumber:(NSString *)text{
    if ((![ValidateCarNum containsArea:[text substringToIndex:1]]) ||
        (![ValidateCarNum validateABC:[text substringWithRange:NSMakeRange(1, 1)]]) ||
        (![ValidateCarNum validate123:[text substringFromIndex:2]]) ||
        ([ValidateCarNum containsIO:text]) ||
        ([ValidateCarNum validateABC:[text substringFromIndex:2]])) {
        return NO;
    }else{
        return YES;
    }
}


+(BOOL)isChinese:(NSString *)text{
    NSString *re=@"[\u4e00-\u9fa5]+";
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",re];
    if ([predicate evaluateWithObject:text]) {
        return YES;
    }else{
        return NO;
    }
}
+(BOOL)containsArea:(NSString *)text{
    NSArray *area=@[@"京",@"津",@"沪",@"渝",@"冀",@"豫",@"云",@"辽",@"黑",@"湘",@"皖",@"鲁",@"新",@"苏",@"浙",@"赣",@"鄂",@"桂",@"甘",@"晋",@"蒙",@"陕",@"吉",@"闽",@"贵",@"粤",@"青",@"藏",@"川",@"宁",@"琼",@"甲",@"乙",@"丙",@"己",@"庚",@"辛",@"壬",@"寅",@"辰",@"戍",@"午",@"未",@"申"];
    if ([area containsObject:text]) {
        return YES;
    }else{
        return NO;
    }
}
+(BOOL)validateABC:(NSString *)text{
    NSString *re=@"[A-Za-z]+";
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",re];
    if ([predicate evaluateWithObject:text]) {
        return YES;
    }else{
        return NO;
    }
}
+(BOOL)validate123:(NSString *)text{
    NSString *re=@"[A-Za-z0-9]+";
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",re];
    if ([predicate evaluateWithObject:text]) {
        return YES;
    }else{
        return NO;
    }
}
+(BOOL)containsIO:(NSString *)text{
    NSRange rangeI=[text rangeOfString:@"I"];
    NSRange rangeO=[text rangeOfString:@"O"];
    if (rangeI.length>0 || rangeO.length>0) {
        return YES;
    }else{
        return NO;
    }
}
+(BOOL)checkLast5IsABC:(NSString *)text{
    return YES;
}
+(BOOL)checkIsEmoji:(NSString *)text{
    // 过滤所有表情。returnValue为NO表示不含有表情，YES表示含有表情
    __block BOOL returnValue = NO;
    [text enumerateSubstringsInRange:NSMakeRange(0, [text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        
        const unichar hs = [substring characterAtIndex:0];
        // surrogate pair
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    returnValue = YES;
                }
            }
        } else if (substring.length > 1) {
            const unichar ls = [substring characterAtIndex:1];
            if (ls == 0x20e3) {
                returnValue = YES;
            }
        } else {
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff) {
                returnValue = YES;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                returnValue = YES;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                returnValue = YES;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                returnValue = YES;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                returnValue = YES;
            }
        }
    }];
    return returnValue;
}
+(void)limitNumber:(NSInteger)kmaxlength Sender:(UITextField *)sender{
    NSInteger kMaxLength=kmaxlength;
    NSString *toBeString = sender.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [sender markedTextRange];       //获取高亮部分
        UITextPosition *position = [sender positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > kMaxLength) {
                sender.text = [toBeString substringToIndex:kMaxLength];
            }
        }       // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }   // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况   else{
    if (toBeString.length > kMaxLength) {
        sender.text = [toBeString substringToIndex:kMaxLength];
    }
}
@end
